#include <Windows.h>
#include <tchar.h>
#include "resource.h"
#include <WindowsX.h>

void AddText(HWND hWnd, TCHAR* szFormat, ...)
{
	va_list valist;
	va_start(valist, szFormat);
	TCHAR szBuff[20 * 1024]= {0};
	Edit_GetText(hWnd, szBuff, 20 * 1024);
	_vstprintf(_tcschr(szBuff, 0), szFormat, valist);
	Edit_SetText(hWnd, szBuff);
	va_end(valist);
}

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0078"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);
			
			SendMessage(hDlg, WM_COMMAND, MAKEWPARAM(IDC_BTNVERSION, BM_CLICK), 0);

			break;
		}
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_BTNVERSION:
				{
					OSVERSIONINFO ovi = {sizeof(OSVERSIONINFO)};
					GetVersionEx(&ovi);
					SetDlgItemText(hDlg, IDC_EDTVERSION, _T(""));
					AddText(GetDlgItem(hDlg, IDC_EDTVERSION),				
						_T("OS Version Infomation:\r\n")
						_T("  %-20s %8d.%d.%d\r\n")
						_T("  %-20s %8u\r\n")
						_T("  %-20s %s\r\n"),						
						_T("Version:"), ovi.dwMajorVersion, ovi.dwMinorVersion, ovi.dwBuildNumber,
						_T("Platform ID:"), ovi.dwPlatformId,
						_T("CSDVersion:"), ovi.szCSDVersion);
					break;
				}
			}
			break;
		}
		case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}